/*
   MIT License

   Copyright (c) 2021 Jordan Vrtanoski

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in all
   copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   SOFTWARE.
   */


#ifndef LUACPP_LUASTATE_HPP
#define LUACPP_LUASTATE_HPP

#include <ostream>

#include "../Lua.hpp"

namespace LuaCpp {
	/**
	 * @brief Low level classes related to the communication with the Lua engine
	 */
	namespace Engine {

		/**
		 * @brief Wrapper of `struct lua_State` defined in the Lua library
		 *
		 * @details
		 * A wrapper of the `struct lua_State` generated by the Lua library
		 * which will be used in the communication with the instance of the
		 * Lua virtual machine.
		 */
		class LuaState {
		private:
			lua_State *L;
			bool shared;
		public:
			/**
			 * @brief Constructor that creates a new state
			 *
			 * @details
			 * Creates new lua_State with the low level call to the 
			 * Lua library. The state should not be closed by the 
			 * user, since the class will autmaticaly close the state
			 * one the instance is destroyed.
			 */
			explicit LuaState();

			/**
			 * @brief Constructor that creates a new state
			 *
			 * @details
			 * Creates new lua_State with the low level call to the 
			 * Lua library. The state should not be closed by the 
			 * user, since the class will autmaticaly close the state
			 * one the instance is destroyed.
			 */
			explicit LuaState(lua_State *_L) : L(_L), shared(false) {}
		
			/**
			 * @brief Constructor that creates a new state
			 *
			 * @details
			 * Creates new lua_State with the low level call to the 
			 * Lua library. The state should not be closed by the 
			 * user, since the class will autmaticaly close the state
			 * one the instance is destroyed.
			 */
			explicit LuaState(lua_State *_L, bool _shared) : L(_L), shared(_shared)  {}
			
			/**
			 * @brief Destructor
			 *
			 * @details
			 * The destructure will close the wrapped `lua_State`
			 */
			~LuaState();
			
			/**
			 * @brief Cast operator converting to lua_State*
			 *
			 * @details
			 * Operator helping the implicit conversion from LuaState to
			 * lua_State *. Allowes the use of the State wrapper object
			 * in place of the struct in the `C` calls.
			 *
			 * @return the pointer to the wrapped state
			 */
			operator lua_State *();

			/**
			 * @brief Returns the pointer to the state
			 *
			 * @details
			 * Returns the pointer to the state that is wrapped.
			 * The state should not be closed by the user since
			 * the closure is done in the destructor of the 
			 * LuaState.
			 */	
			lua_State * getState();

			/**
			 * @brief Print the stack content on a stream
			 *
			 * @details
			 * Prints the content of the stack on an outpout stream
			 * for debugging purpose.
			 *
			 * @param out Stram on which the stack will be printed
			 */
			void PrintStack(std::ostream &out); 
		};
	}
}

#endif // LUACPP_LUASTATE_HPP
